package com.zengqingfa.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zengqingfa.demo.annotation.AspectAnnotation;
import com.zengqingfa.demo.entity.SysUser;
import com.zengqingfa.demo.interceptor.MyInterceptor;
import com.zengqingfa.demo.service.UserService;
import com.zengqingfa.demo.service.impl.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author zengqingfa
 * @date 2019/10/14 14:21
 * @description
 * @email zengqingfa_java@163.com
 */

@RequestMapping("/user")
@RestController
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getById")
    public SysUser getById(Long id) {
        log.info("开始处理任务4");
        SysUser sysUser = userService.getById(id);
        return sysUser;
    }


    @RequestMapping("/getUserList")
    public PageInfo getUserList(HttpServletRequest request) {

        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        pageNo = pageNo == null || pageNo.trim().length() == 0 ? "1" : pageNo;
        pageSize = pageSize == null || pageSize.trim().length() == 0 ? "2" : pageSize;
        Integer pageNo1 = Integer.parseInt(pageNo);
        Integer pageSize1 = Integer.parseInt(pageSize);
        PageHelper.startPage(pageNo1, pageSize1);//设置分页的起始码以及页面大小
        List<SysUser> userList = userService.findAll();
        PageInfo pageInfo = new PageInfo(userList);//传入list就可以了
        return pageInfo;
    }

    @RequestMapping("/testTransaction1")
    public String testTransaction1() {
        try {
            userService.testTransaction1();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping("/testTransaction2")
    public String testTransaction2() {
        try {
            userService.testTransaction2();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

}
